package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.parameter.SaveSelectionConditionBean;
import com.shop2cn.iapi.agentproducts.service.SaveSelectionConditionCall;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.getInviteDistributorPicBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.getInviteDistributorPicCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.agentrebate.Ymt_ProductSelectionCondition;
import com.ymttest.database.model.agentrebate.Ymt_ProductSelectionConditionExample;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*创建/修改下级商品输出筛选条件Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SaveSelectionCondition{
	private static SaveSelectionConditionBean saveselectionconditionBean;
	private static SaveSelectionConditionCall saveselectionconditionCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建/修改下级商品输出筛选条件Test");
	}

	@Before
	public void caseUp() {
		saveselectionconditionBean=new SaveSelectionConditionBean();
		saveselectionconditionCall=new SaveSelectionConditionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveselectionconditionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private String getListToString(List list) {
		String s="";
		for (int i = 0; i < list.size(); i++) {
			s+=list.get(i);
		}
		return s;
		
	}
	private void checkDB(SaveSelectionConditionBean saveselectionconditionBean2) {
		Ymt_ProductSelectionConditionExample example5=new Ymt_ProductSelectionConditionExample();
		example5.createCriteria().andUserIdEqualTo(saveselectionconditionBean2.getUserId());
		List<Ymt_ProductSelectionCondition> ct = SqlMapperFactory.getIntergratedProductMapper().selectYmtProductSelectionConditionExample(example5);
		System.out.println(FormatUtil.beanToGSONString(ct));
		Logger.verifyEquals(0, ct.get(0).getAction(), "验证有效");
		Logger.verifyEquals(saveselectionconditionBean2.getAgentId(), ct.get(0).getAgentId(), "验证getAgentId");
		if(saveselectionconditionBean2.getScope()==0) {
			Logger.verifyIsNull(ct.get(0).getBrandIdList(), "验证getBrandIdList");
			Logger.verifyIsNull(ct.get(0).getDepotIdList(), "验证getDepotIdList");
			Logger.verifyIsNull(ct.get(0).getFirstCategoryIdList(), "验证getFirstCategoryIdList");
		}else if(saveselectionconditionBean2.getScope()==1) {
			if(saveselectionconditionBean2.getBrandIdList()!=null) {
				Logger.verifyEquals(getListToString(saveselectionconditionBean2.getBrandIdList()),ct.get(0).getBrandIdList(), "验证getBrandIdList");
			}
			if(saveselectionconditionBean2.getDepotIdList()!=null) {
				Logger.verifyEquals(getListToString(saveselectionconditionBean2.getDepotIdList()),ct.get(0).getDepotIdList(), "验证getDepotIdList");
			}
			if(saveselectionconditionBean2.getFirstCategoryIdList()!=null) {
				Logger.verifyEquals(getListToString(saveselectionconditionBean2.getFirstCategoryIdList()),ct.get(0).getFirstCategoryIdList(), "验证getFirstCategoryIdList");
			}
		}
	}
	private int MchId=501163178;
	private int AgentId = 501163179;
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveSelectionCondition_001() {
		Logger.start(true,"设置按照品牌查询，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			saveselectionconditionBean.setUserId(MchId);
			saveselectionconditionBean.setAgentId(AgentId);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveSelectionCondition_002() {
		Logger.start(true,"设置查询全部商品，成功");
		try {
			saveselectionconditionBean.setUserId(MchId);
			saveselectionconditionBean.setAgentId(AgentId);
			saveselectionconditionBean.setScope(0);
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveSelectionCondition_003() {
		Logger.start(true,"设置按照仓库，成功");
		try {
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByMchId(MchId);
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			saveselectionconditionBean.setUserId(MchId);
			saveselectionconditionBean.setAgentId(AgentId);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setDepotIdList(Arrays.asList(addAgentProductBean.getDepotId()));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveSelectionCondition_004() {
		Logger.start(true,"设置全部商品，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			
			saveselectionconditionBean.setUserId(MchId);
			saveselectionconditionBean.setAgentId(AgentId);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setFirstCategoryIdList(Arrays.asList(addAgentProductBean.getCategoryId()));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//Tc_registerByCode_002
	public void Tc_SaveSelectionCondition_005() {
		Logger.start(true,"设置按照品牌查询，成功-自动导入商品");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 2);
			Integer mchId_Agent = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			saveselectionconditionBean.setUserId(mchId);
			saveselectionconditionBean.setAgentId(mchId_Agent);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			addAgentProductBean2.setBrandId(addAgentProductBean.getBrandId()+1);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			AddAgentProductBean addAgentProductBean3 = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			String productId3 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean3);
			
			Thread.sleep(5000);
			  //验证导入商品
            List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(mchId_Agent);
            Logger.verifyEquals(true, getProductLists2.stream().filter(x->x.getProductId().equals(productId)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists2.stream().filter(x->x.getProductId().equals(productId2)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(true, getProductLists2.stream().filter(x->x.getProductId().equals(productId3)).findAny().isPresent(), "导入商品成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//Tc_registerByCode_002
	public void Tc_SaveSelectionCondition_006() {
		Logger.start(true,"设置条件前已导入商品不满足条件,移除");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
			Integer mchId_Agent = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(mchId_Agent, "", productId, mchId, false);
			AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			Thread.sleep(5000);
			  //验证导入商品
          List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(mchId_Agent);
          getProductLists2.forEach(x->{
          	Logger.debug(FormatUtil.beanToGSONString(x));
          });
          List<String> p=getProductLists2.stream().map(x->x.getProductId()).collect(Collectors.toList());
          
          Logger.verifyEquals(1, p.size(), "验证导入商品");
			saveselectionconditionBean.setUserId(mchId);
			saveselectionconditionBean.setAgentId(mchId_Agent);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()+1));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			Thread.sleep(5000);
			  //验证导入商品
             getProductLists2 = AddAgentProductCallService.GetProductLists(mchId_Agent);
            getProductLists2.forEach(x->{
            	Logger.debug(FormatUtil.beanToGSONString(x));
            });
            List<String> p2=getProductLists2.stream().map(x->x.getProductId()).collect(Collectors.toList());
            
            Logger.verifyEquals(0, p2.size(), "验证导入商品移除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//Tc_registerByCode_002
	public void Tc_SaveSelectionCondition_007() {
		Logger.start(true,"设置按照品牌查询，成功-自动导入商品-修改");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 2);
			Integer mchId_Agent = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			createDepotBean bean=GodmchManageCallService.createDepot(mchId);
            List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());

			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			saveselectionconditionBean.setUserId(mchId);
			saveselectionconditionBean.setAgentId(mchId_Agent);
			saveselectionconditionBean.setScope(1);
			saveselectionconditionBean.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			saveselectionconditionCall.setData(saveselectionconditionBean);
			saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			addAgentProductBean2.setBrandId(addAgentProductBean.getBrandId()+1);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			AddAgentProductBean addAgentProductBean3 = AddAgentProductCallService.initAddAgentProduct(mchId, "", true);
			String productId3 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean3);
			
			Thread.sleep(5000);
			  //验证导入商品
            List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(mchId_Agent);
            Logger.verifyEquals(true, getProductLists2.stream().filter(x->x.getProductId().equals(productId)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists2.stream().filter(x->x.getProductId().equals(productId2)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(true, getProductLists2.stream().filter(x->x.getProductId().equals(productId3)).findAny().isPresent(), "导入商品成功");
            
            //验证条件FirstCategoryIdList
            saveselectionconditionBean.setFirstCategoryIdList(Arrays.asList(addAgentProductBean.getCategoryId()+1000));
            saveselectionconditionBean.setBrandIdList(null);
            saveselectionconditionBean.setDepotIdList(null);
            saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			Thread.sleep(5000);
			  //验证导入商品
            List<ProductListDto> getProductLists3 = AddAgentProductCallService.GetProductLists(mchId_Agent);
            Logger.verifyEquals(false, getProductLists3.stream().filter(x->x.getProductId().equals(productId)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists3.stream().filter(x->x.getProductId().equals(productId2)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists3.stream().filter(x->x.getProductId().equals(productId3)).findAny().isPresent(), "导入商品成功");

          //验证DepotId
            saveselectionconditionBean.setFirstCategoryIdList(null);
            saveselectionconditionBean.setBrandIdList(null);
            saveselectionconditionBean.setDepotIdList(Arrays.asList(addAgentProductBean.getDepotId()));
            saveselectionconditionCall.callService();			
			Logger.verifyEquals("200", saveselectionconditionCall.getString("code"), "验证code");
			checkDB(saveselectionconditionBean);
			Thread.sleep(5000);
			  //验证导入商品
            List<ProductListDto> getProductLists4 = AddAgentProductCallService.GetProductLists(mchId_Agent);
            Logger.verifyEquals(true, getProductLists4.stream().filter(x->x.getProductId().equals(productId)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists4.stream().filter(x->x.getProductId().equals(productId2)).findAny().isPresent(), "导入商品成功");
            Logger.verifyEquals(false, getProductLists4.stream().filter(x->x.getProductId().equals(productId3)).findAny().isPresent(), "导入商品成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}